Python plays GTA San Andreas error while test_model.py with model

by: koolhussain, 7 years ago


Code is written in Python 3.5.X

Please try to make the answer simple for a 3rd year Computer Science Student

The output files from train_model.py seems to be a model.meta file but the test_model.py is asking for a .model file. The tutorial user has a .model file as well I can't seem to understand why i am getting a file with .model.meta

I am trying to play GTA San Andreas through Python or more precisely the car in GTA is driven by the model. It takes screen frames as Input and recorded the key i Input during training. This training data is used to train the model.

Code for training the model


import numpy as np
from alexnet import alexnet

WIDTH = 80
HEIGHT = 60
LR = 1e-3
EPOCHS = 8
MODEL_NAME = 'pygta_sa-car-{}-{}-{}-epochs.model'.format(LR, 'alextnetv2', EPOCHS)

model = alexnet(WIDTH, HEIGHT, LR)

train_data = np.load('training_data_v2.npy')

train = train_data[:-500]
test = train_data[-500:]

X = np.array([i[0] for i in train]).reshape(-1,WIDTH,HEIGHT,1)
Y = [i[1] for i in train]

test_x = np.array([i[0] for i in test]).reshape(-1,WIDTH,HEIGHT,1)
test_y = [i[1] for i in test]

model.fit({'input': X}, {'targets': Y}, n_epoch=EPOCHS, validation_set=({'input': test_x}, {'targets': test_y}),
    snapshot_step=500, show_metric=True, run_id=MODEL_NAME)

# tensorboard --logdir=foo:F:play_gta_salog

model.save(MODEL_NAME)


Training of Model completed sucessfully with EPOCHS 8 and 15 and return files are:-
pygta_sa-car-0.001-alexnetv2-15-epochs.model.data-00000-of-00001
pygta_sa-car-0.001-alexnetv2-15-epochs.model.index
pygta_sa-car-0.001-alexnetv2-15-epochs.model.meta

Content of Checkpoint file

model_checkpoint_path: "F:play_gta_sapygta_sa-car-0.001-alextnetv2-15-epochs.model" all_model_checkpoint_paths: "F:play_gta_sapygta_sa-car-0.001-alextnetv2-15-epochs.model"

Code for Test_model.py


import numpy as np
import cv2
import time
from grabscreen import grab_screen
from getkeys import key_check
from directkeys import PressKey, ReleaseKey, W, A, S, D
from alexnet import alexnet


WIDTH = 80
HEIGHT = 60
LR = 1e-3
EPOCHS = 8
MODEL_NAME = 'pygta_sa-car-{}-{}-{}-epochs.model'.format(LR, 'alexnetv2',EPOCHS)


def straight():
    PressKey(W)
    ReleaseKey(A)
    ReleaseKey(D)

def left():
    PressKey(W)
    PressKey(A)
    ReleaseKey(D)

def right():
    PressKey(W)
    PressKey(D)
    ReleaseKey(A)

model = alexnet(WIDTH, HEIGHT, LR)
model.load(MODEL_NAME)

def main():

    for i in list(range(10))[::-1]:
        print(i+1)
        time.sleep(1)

    last_time = time.time()

    paused = False

    while True:
        if not paused:

            screen = grab_screen(region=(0,40,800,640))
            screen = cv2.cvtColor(screen,cv2.COLOR_BGR2GRAY)
            screen = cv2.resize(screen,(80,60))
            print('Frame took {} seconds'.format(time.time()-last_time))
            last_time = time.time()

            moves = list(np.around(model.predict([screen.reshape(80,60,1)])[0]))
            print(moves, prediction)

            if moves == [1,0,0]:
                left()
            elif moves == [0,1,0]:
                straight()
            elif moves == [0,0,1]:
                right()

        keys = key_check()

    # p pauses game and can get annoying.
        if 'T' in keys:
            if paused:
                paused = False
                time.sleep(1)
            else:
                paused = True
                ReleaseKey(A)
                ReleaseKey(W)
                ReleaseKey(D)
                time.sleep(1)

main()


The Error Message on running test_model.py


Traceback (most recent call last):
  File "C:Program FilesPython35libsite-packagestensorflowpythonclientsession.py", line 1039, in _do_call
    return fn(*args)
  File "C:Program FilesPython35libsite-packagestensorflowpythonclientsession.py", line 1021, in _run_fn
    status, run_metadata)
  File "C:Program FilesPython35libcontextlib.py", line 66, in __exit__
    next(self.gen)
  File "C:Program FilesPython35libsite-packagestensorflowpythonframeworkerrors_impl.py", line 466, in raise_exception_on_not_ok_status
    pywrap_tensorflow.TF_GetCode(status))
tensorflow.python.framework.errors_impl.NotFoundError: Unsuccessful TensorSliceReader constructor: Failed to find any matching files for F:play_gta_sapygta_sa-car-0.001-alexnetv2-8-epochs.model
     [[Node: save_1/RestoreV2 = RestoreV2[dtypes=[DT_FLOAT], _device="/job:localhost/replica:0/task:0/cpu:0"](_recv_save_1/Const_0, save_1/RestoreV2/tensor_names, save_1/RestoreV2/shape_and_slices)]]

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "F:play_gta_satest_model.py", line 33, in <module>
    model.load(MODEL_NAME)
  File "C:Program FilesPython35libsite-packagestflearnmodelsdnn.py", line 282, in load
    self.trainer.restore(model_file, weights_only, **optargs)
  File "C:Program FilesPython35libsite-packagestflearnhelperstrainer.py", line 452, in restore
    self.restorer.restore(self.session, model_file)
  File "C:Program FilesPython35libsite-packagestensorflowpythontrainingsaver.py", line 1457, in restore
    {self.saver_def.filename_tensor_name: save_path})
  File "C:Program FilesPython35libsite-packagestensorflowpythonclientsession.py", line 778, in run
    run_metadata_ptr)
  File "C:Program FilesPython35libsite-packagestensorflowpythonclientsession.py", line 982, in _run
    feed_dict_string, options, run_metadata)
  File "C:Program FilesPython35libsite-packagestensorflowpythonclientsession.py", line 1032, in _do_run
    target_list, options, run_metadata)
  File "C:Program FilesPython35libsite-packagestensorflowpythonclientsession.py", line 1052, in _do_call
    raise type(e)(node_def, op, message)
tensorflow.python.framework.errors_impl.NotFoundError: Unsuccessful TensorSliceReader constructor: Failed to find any matching files for F:play_gta_sapygta_sa-car-0.001-alexnetv2-8-epochs.model
     [[Node: save_1/RestoreV2 = RestoreV2[dtypes=[DT_FLOAT], _device="/job:localhost/replica:0/task:0/cpu:0"](_recv_save_1/Const_0, save_1/RestoreV2/tensor_names, save_1/RestoreV2/shape_and_slices)]]

Caused by op 'save_1/RestoreV2', defined at:
  File "<string>", line 1, in <module>
  File "C:Program FilesPython35libidlelibrun.py", line 124, in main
    ret = method(*args, **kwargs)
  File "C:Program FilesPython35libidlelibrun.py", line 351, in runcode
    exec(code, self.locals)
  File "F:play_gta_satest_model.py", line 32, in <module>
    model = alexnet(WIDTH, HEIGHT, LR)
  File "F:play_gta_saalexnet.py", line 40, in alexnet
    max_checkpoints=1, tensorboard_verbose=0, tensorboard_dir='log')
  File "C:Program FilesPython35libsite-packagestflearnmodelsdnn.py", line 64, in __init__
    best_val_accuracy=best_val_accuracy)
  File "C:Program FilesPython35libsite-packagestflearnhelperstrainer.py", line 147, in __init__
    allow_empty=True)
  File "C:Program FilesPython35libsite-packagestensorflowpythontrainingsaver.py", line 1056, in __init__
    self.build()
  File "C:Program FilesPython35libsite-packagestensorflowpythontrainingsaver.py", line 1086, in build
    restore_sequentially=self._restore_sequentially)
  File "C:Program FilesPython35libsite-packagestensorflowpythontrainingsaver.py", line 691, in build
    restore_sequentially, reshape)
  File "C:Program FilesPython35libsite-packagestensorflowpythontrainingsaver.py", line 407, in _AddRestoreOps
    tensors = self.restore_op(filename_tensor, saveable, preferred_shard)
  File "C:Program FilesPython35libsite-packagestensorflowpythontrainingsaver.py", line 247, in restore_op
    [spec.tensor.dtype])[0])
  File "C:Program FilesPython35libsite-packagestensorflowpythonopsgen_io_ops.py", line 669, in restore_v2
    dtypes=dtypes, name=name)
  File "C:Program FilesPython35libsite-packagestensorflowpythonframeworkop_def_library.py", line 768, in apply_op
    op_def=op_def)
  File "C:Program FilesPython35libsite-packagestensorflowpythonframeworkops.py", line 2336, in create_op
    original_op=self._default_original_op, op_def=op_def)
  File "C:Program FilesPython35libsite-packagestensorflowpythonframeworkops.py", line 1228, in __init__
    self._traceback = _extract_stack()

NotFoundError (see above for traceback): Unsuccessful TensorSliceReader constructor: Failed to find any matching files for F:play_gta_sapygta_sa-car-0.001-alexnetv2-8-epochs.model
     [[Node: save_1/RestoreV2 = RestoreV2[dtypes=[DT_FLOAT], _device="/job:localhost/replica:0/task:0/cpu:0"](_recv_save_1/Const_0, save_1/RestoreV2/tensor_names, save_1/RestoreV2/shape_and_slices)]]


I am not getting a file with only .model extension so, what seems to be the problem.





You must be logged in to post. Please login or register an account.